﻿#include "Global.h"

static int speed_arr[] =
{
    921600, 460800, 230400, 115200,
    57600,  38400,  19200,  9600,
    4800,   2400,   1800,   1200,
    600,    300,    150,    110,
    75,     50
};

static int baudflag_arr[] =
{
    B921600, B460800, B230400, B115200,
    B57600,  B38400,  B19200,  B9600,
    B4800,   B2400,   B1800,   B1200,
    B600,    B300,    B150,    B110,
    B75,     B50
};

void onErrorInfo(QLabel *TextEdit, int Error_Num)
{
    if( chargingData->devtype == DC )
    {
        switch(Error_Num)
        {
        case DC_IND_OTHER_FAULT:TextEdit->setText(QObject::tr("充电桩故障-其他故障")); break;
        case DC_IND_EMERSTOP_FAULT:TextEdit->setText(QObject::tr("充电桩故障-急停按钮动作故障")); break;
        case DC_IND_SMOKE_ALARM:TextEdit->setText(QObject::tr("充电桩故障-烟雾传感器感故障"));break;
        case DC_IND_AC_BREAKER:TextEdit->setText(QObject::tr("充电桩故障-交流输入断路器故障"));break;
        case DC_IND_DC_OUT_CONTACTOR:TextEdit->setText(QObject::tr("充电桩故障-直流母线输出接触器拒动/误动故障"));break;
        case DC_IND_DC_OUT_FUSES:TextEdit->setText(QObject::tr("充电桩故障-直流母线输出熔断器故障"));break;
        case DC_IND_INTERFACE_ELE_LOCK_FAULT:TextEdit->setText(QObject::tr("充电桩故障-充电接口电子锁故障"));break;
        case DC_IND_CHARGE_FAN_FAULT:TextEdit->setText(QObject::tr("充电桩故障-充电桩风扇故障")); break;
        case DC_IND_THUNDERBOLT:TextEdit->setText(QObject::tr("充电桩故障-避雷器故障")); break;
        case DC_IND_INSULATION:TextEdit->setText(QObject::tr("充电桩故障-绝缘监测故障")); break;
        case DC_IND_BATTERY_REVERSE:TextEdit->setText(QObject::tr("充电桩故障-电池极性反接故障")); break;
        case DC_IND_CHARGING_CAR_GUIDE:TextEdit->setText(QObject::tr("充电桩故障-充电中车辆控制导引故障")); break;
        case DC_IND_PILE_OVERTURNED:TextEdit->setText(QObject::tr("充电桩故障-充电桩过温故障")); break;
        case DC_IND_GUNS_OVERTURNED:TextEdit->setText(QObject::tr("充电桩故障-充电接口过温故障")); break;
        case DC_IND_GUNS_NO_HOMELESS:TextEdit->setText(QObject::tr("充电桩故障-充电枪未归位告警")); break;
        case DC_IND_BMS_COMMUNICATION:TextEdit->setText(QObject::tr("充电桩故障-BMS通信故障")); break;
        case DC_IND_INPUT_V_OVER:TextEdit->setText(QObject::tr("充电桩故障-输入电压过压故障")); break;
        case DC_IND_INPUT_V_UNDER:TextEdit->setText(QObject::tr("充电桩故障-输入电压欠压故障"));  break;
        case DC_IND_DC_BUS_OUT_V_OVER:TextEdit->setText(QObject::tr("充电桩故障-直流母线输出过压故障")); break;
        case DC_IND_DC_BUS_OUT_V_UNDER:TextEdit->setText(QObject::tr("充电桩故障-直流母线输出欠压故障")); break;
        case DC_IND_DC_BUS_OUT_A_OVER:TextEdit->setText(QObject::tr("充电桩故障-直流母线输出过流故障")); break;
        case DC_IND_CHARGE_MODULE:TextEdit->setText(QObject::tr("充电桩故障-充电模块故障")); break;
        case DC_IND_MODULE_AC_INPUT:TextEdit->setText(QObject::tr("充电桩故障-充电模块交流输入故障"));break;
        case DC_IND_MODULE_AC_IN_V_OVER:TextEdit->setText(QObject::tr("充电桩故障-充电模块交流输入过压故障")); break;
        case DC_IND_MODULE_AC_IN_V_UNDER:TextEdit->setText(QObject::tr("充电桩故障-充电模块交流输入欠压故障")); break;
        case DC_IND_MODULE_AC_IN_PHASE:TextEdit->setText(QObject::tr("充电桩故障-充电模块交流输入缺相故障")); break;
        case DC_IND_MODULE_DC_OUT_SHORT:TextEdit->setText(QObject::tr("充电桩故障-充电模块直流输出短路故障")); break;
        case DC_IND_MODULE_DC_OUT_A_OVER:TextEdit->setText(QObject::tr("充电桩故障-充电模块直流输出过流故障")); break;
        case DC_IND_MODULE_DC_OUT_V_OVER:TextEdit->setText(QObject::tr("充电桩故障-充电模块直流输出过压故障")); break;
        case DC_IND_MODULE_DC_OUT_V_UNDER:TextEdit->setText(QObject::tr("充电桩故障-充电模块直流输出欠压故障")); break;
        case DC_IND_MODULE_OVERTURNED:TextEdit->setText(QObject::tr("充电桩故障-充电模块过温故障")); break;
        case DC_IND_MODULE_COMMUNICATION:TextEdit->setText(QObject::tr("充电桩故障-充电模块通信故障")); break;
        case DC_IND_MODULE_FAN:TextEdit->setText(QObject::tr("充电桩故障-充电模块风扇故障")); break;
        case DC_IND_DOOR_FAULT:TextEdit->setText(QObject::tr("充电桩故障-门禁故障"));break;
        case DC_IND_DC_BUS_OUT_CONTACTOR_ADHESION_FAULT:TextEdit->setText(QObject::tr("充电桩故障-直流输出接触器粘连故障")); break;
        case DC_IND_INSULATION_WARN:TextEdit->setText(QObject::tr("充电桩故障-绝缘监测告警"));break;
        case DC_IND_DISCHARGE_LOOP_WARN:TextEdit->setText(QObject::tr("充电桩故障-泄放回路故障"));break;
        case DC_IND_CHARGE_PILE_OVERTURNED_WARN:TextEdit->setText(QObject::tr("充电桩故障-充电桩过温告警")); break;
        case DC_IND_CHARGE_GUN_OVERTURNED_WARN:TextEdit->setText(QObject::tr("充电桩故障-充电枪过温告警")); break;
        case DC_IND_AC_IN_CONTACTOR_ERR_ACTION:TextEdit->setText(QObject::tr("充电桩故障-交流输入接触器据动/误动故障")); break;
        case DC_IND_AC_IN_CONTACTOR_ADHESION_FAULT:TextEdit->setText(QObject::tr("充电桩故障-交流输入接触器粘连故障")); break;
        case DC_IND_ASSIST_POWER_FAULT:TextEdit->setText(QObject::tr("充电桩故障-辅助电源故障")); break;
        case DC_IND_PARALLEL_CONTACTOR_ERR_ACTION:TextEdit->setText(QObject::tr("充电桩故障-并联接触器据动/误动故障")); break;
        case DC_IND_PARALLEL_CONTACTOR_ADHESION_FAULT:TextEdit->setText(QObject::tr("充电桩故障-并联接触器粘连故障")); break;
        case DC_IND_CAN_COMMUNICATION_FAULT:TextEdit->setText(QObject::tr("充电桩故障-CAN通信故障")); break;
        case DC_IND_AMMATER_FAULT:TextEdit->setText(QObject::tr("充电桩故障-电表故障")); break;
        case DC_IND_CARD_READER_FAULT:TextEdit->setText(QObject::tr("充电桩故障-读卡器故障")); break;
      default:
        TextEdit->clear();
        }
    }
}
pthread_mutex_t mutex;//必须作为全局变量存在读卡器才能正常运行

rf_card* initCard(rf_card *card)
{
    Q_UNUSED(speed_arr);
    Q_UNUSED(baudflag_arr);

    sem_t sem;
    pthread_mutex_init(&mutex, NULL);
    sem_init(&sem, 0, 0);

    do{
        card = (rf_card*)malloc(sizeof(rf_card));
    }while(card == NULL);

    strcpy(card->com.name,"/dev/ttyS5");
    card->com.baud = 115200;
    card->com.data_bits = 8;
    card->com.parity = 0;
    card->com.stop_bits = 1;

    int ret = protocol_card_init(card,&mutex,&sem);
    if( ret == 0 )
    {
        printf("初始化卡成功：%p@@@@!!!\n", card);
    }
    else
    {
        printf("初始化卡失败!!!\n");
    }
    // protocol_seek_card

    return card;
}

void Init_Focus()
{
    focus_gun_ = 0;//焦点初始化
    stackLayout->setCurrentIndex(ChooseGunPage);
}
QString onGunQRCodeInfo(QString gunCode,int gunCode_, int m_num, int gunNum)
{
    QString QrCodeNum;
    QrCodeNum.clear();
    QrCodeNum = readFile(QString("/usr/local/sanway/dbTest/charge.conf"), QString("propertyCode"));
    QrCodeNum.append(gunCode);
//#if (PRODUCER != 1)
    QrCodeNum.append(QString::number(gunCode_));
//#endif
    chargingData->gunmsg[m_num].guntab = gunNum + 1;
//    gun_label = gunNum+1;
    qDebug() << "结尾是: " << gunCode << "; 当前的二维码是: " << QrCodeNum;
    return QrCodeNum;
}

void Back_Stand_By()//返回待机状态,待机页面,无焦点枪
{
    focus_gun_ = 0;
    stackLayout->setCurrentIndex(ChooseGunPage);
}

// para: {gun_idx, charge_mod[full,time,money,power], require, card_vaild, val[0, min*100, rmb*100, power*100], gun_no, vin_charge}

void onStartingCharging(int m_num, char m_mode, char m_state,
                        char m_checkResult, int m_value, char m_guntab,
                        char VINState)
{
    // swipe card info
    chargingData->cmd2[m_num].result = 0;
    chargingData->cmd2[m_num].mod = m_mode;
    if(VINState == 0)
    {
        chargingData->cmd2[m_num].state = m_state;
    }
    else
    {
        chargingData->cmd2[m_num].state = 0;
        chargingData->cmd7[m_num].req_state = 1;
        chargingData->cmd7[m_num].guntab = m_guntab;
    }
    chargingData->cmd2[m_num].check_result = m_checkResult;
    chargingData->cmd2[m_num].value = m_value;
    chargingData->cmd2[m_num].guntab = m_guntab;
    chargingData->cmd4[m_num].req_state = 0;
    chargingData->cmd4[m_num].result = 0;
    qDebug("卡片认证状态: chargingData->cmd2[%d].check_result = %d \n", m_num, chargingData->cmd2[m_num].check_result);
    qDebug("充电模式: chargingData->cmd2[%d].mod = %d \n", m_num, chargingData->cmd2[m_num].mod);
    qDebug("充电参数: chargingData->cmd2[%d].value = %d \n", m_num, chargingData->cmd2[m_num].value);
    qDebug("请求状态: chargingData->cmd2[%d].state = %d \n", m_num, chargingData->cmd2[m_num].state);
    chargingData->cmd4[m_num].stopReason = 0;
//    if( !labelStopReason->text().isEmpty() )
//    {
//        labelStopReason->clear();
//    }
}

/****************************************************************************
*   功能：将一个 ASCII 码表示的十六进制字符串转换成十六进制的字节串
*   输入参数：pAscii -- 转换后的 ASCII 码表示的十六进制字符串的首地址
*            pHex   -- 十六进制数字节串首地址
*            nLen   -- 要转换的 ASCII 码表示的十六进制字符串的长度（字节数）
*   输出参数：None
*   作者: 余道伟
*   注：  要求输入的 ASCII 码表示的十六进制数的字符个数必须为偶数，除了是1 - 9 和 A(a) - F(f) 以外没有别的字符
****************************************************************************/
void AsciiToHex(char * pAscii, unsigned char * pHex, int nLen)
{
    if (nLen % 2)
        return;
    int nHexLen = nLen / 2;

    for (int i = 0; i < nHexLen; i++)
    {
        unsigned char Nibble[2];
        Nibble[0] = *pAscii++;
        Nibble[1] = *pAscii++;
        for (int j = 0; j < 2; j++)
        {
            if (Nibble[j] <= 'F' && Nibble[j] >= 'A')
                Nibble[j] = Nibble[j] - 'A' + 10;
            else if (Nibble[j] <= 'f' && Nibble[j] >= 'a')
                Nibble[j] = Nibble[j] - 'a' + 10;
            else if (Nibble[j] >= '0' && Nibble[j] <= '9')
                Nibble[j] = Nibble[j] - '0';
            else
                return;
        }   // for (int j = ...)
        pHex[i] = Nibble[0] << 4;   // Set the high nibble
        pHex[i] |= Nibble[1];   //Set the low nibble
    }   // for (int i = ...)
}
/************************************************************************
*   功能：将一个十六进制字节串转换成 ASCII 码表示的十六进制的字符串
*   输入参数：pHex    -- 十六进制数字节串首地址
*            pAscii -- 转换后的 ASCII 码表示的十六进制字符串的首地址
*            nLen    -- 要转换的十六进制数的长度（字节数）
*   输出参数：None
*   作者: 余道伟
*   注：  转换后的结果全部是大写 ASCII 表示的十六进制数
************************************************************************/
void HexToAscii(unsigned char * pHex, unsigned char * pAscii, int nLen)
{
    unsigned char Nibble[2];

    for (int i = 0; i < nLen; i++)
    {
        Nibble[0] = (pHex[i] & 0xF0) >> 4;
        Nibble[1] = pHex[i] & 0x0F;
        for (int j = 0; j < 2; j++)
        {
            if (Nibble[j] < 10)
                Nibble[j] += 0x30;
            else
            {
                if (Nibble[j] < 16)
                    Nibble[j] = Nibble[j] - 10 + 'A';
            }
            *pAscii++ = Nibble[j];
        }   // for (int j = ...)
    }   // for (int i = ...)
}
